from subprocess import Popen
from pickle import dump,load
from string import letters
from random import choice
from os import remove

def fitness(x):
  filename = ''.join(choice(letters) for i in xrange(12))+'.pkl'
  myfile=open(filename,'wb')
  dump(x, myfile)
  myfile.close()
  Popen(["/usr/bin/python", "izh_eval.py", filename],shell=False,stdout=None,stderr=None).wait()
  myfile=open(filename,'rb')
  x=load(myfile)
  myfile.close()
  remove(filename)
  return x

  

##### do not touch this part !
from multiprocessing import Pool,cpu_count
from pickle import load,dump
from time import sleep
from sys import argv

if __name__ == '__main__':
  pop_file=open(argv[1],'r')
  pop=load(pop_file)
  pop_file.close()
  pool = Pool(processes=cpu_count())
  print len(pop)
  fitness=pool.map(fitness,pop)
  assert(len(argv)>1)
  pop_file=open(argv[1],'w')
  assert(len(fitness)==len(pop))
  dump([(pop[i],fitness[i]) for i in range(len(pop))],pop_file)


